package com.open.cinema.system.manager;

import com.open.cinema.common.constant.Constants;
import com.open.cinema.common.model.SysLoginInfo;
import com.open.cinema.common.util.IpUtil;
import org.apache.log4j.Logger;

import java.util.TimerTask;

/**
 * timeTask 任务工厂
 */
public class TimerTaskFactory {

    private static final Logger log = Logger.getLogger(TimerTaskFactory.class);

    public static TimerTask recordLogininfor(final String username, final String status, final String message, final Object... args)
    {
        final String ip = IpUtil.getIp();
        return new TimerTask()
        {
            @Override
            public void run()
            {
                String msg= "ip："+ ip + ",username:"+username +",status:"+ status +",message : " +message;
                log.info(msg);
                // 创建日志对象
                SysLoginInfo sysLoginInfo = new SysLoginInfo();
                sysLoginInfo.setLoginName(username);
                sysLoginInfo.setIpaddr(ip);
                sysLoginInfo.setLoginLocation(IpUtil.getRealAddressByIP(ip));
                sysLoginInfo.setMsg(message);
                // 日志状态
                if (Constants.LOGIN_SUCCESS.equals(status) || Constants.LOGOUT.equals(status))
                {
                    sysLoginInfo.setStatus(Constants.SUCCESS);
                }
                else if (Constants.LOGIN_FAIL.equals(status))
                {
                    sysLoginInfo.setStatus(Constants.FAIL);
                }
                // 插入数据
//                SpringContextUtil.getRequiredBean(SysLoginInfoService.class).insertLogininfor(sysLoginInfo);
            }
        };
    }


}
